import pyautogui
import pydirectinput
import aircv as ac
import numpy as np
import cv2

escortPlace = 3 # 0:东岳；1:杭州；2:开封；3:徐海
choice_addr = {0:-45,1:-20,2:10,3:40} # 0:东岳；1:杭州；2:开封；3:徐海

def isOnScreen(filename):
    pic = pyautogui.locateOnScreen(filename)
    if(pic == None):
        return False
    else:
        return True

def get_XY_color(x,y):
    return pyautogui.screenshot().getpixel((x, y)) 

def IsEscortOK():
    a = get_XY_color(860,400)
    b = get_XY_color(1056,400)
    c = get_XY_color(895,456)
    d = get_XY_color(1036,458)
    x=0
    if(a[0] == 33 and a[1] == 48 and a[2] == 65):
        x+=1
    if(b[0] == 30 and b[1] == 49 and b[2] == 63):
        x+=1
    if(c[0] == 150 and c[1] == 195 and c[2] == 200):
        x+=1
    if(d[0] == 223 and d[1] == 253 and d[2] == 252):
        x+=1
    if(x==4):
        return True
    else:
        return False
    
def moveMouseTo(x, y):
    pydirectinput.moveTo(x, y)

def moveMouseAndClick(x, y):
    moveMouseTo(x, y)
    pydirectinput.click()

running_log = open("textfile.txt","w")

temp_XY = (0, 0)
def matchImg(filePath):
    screenshotIm = pyautogui.screenshot()
    imgsrc = np.array(screenshotIm)  # 打开查找页(全屏)
    imgobj = ac.imread(filePath)  # 打开待识别的图片
    match_result = ac.find_all_template(imgsrc, imgobj, 0.9)  # 0.9是识别达标率
    if(len(match_result) != 0):
        global temp_XY
        temp_XY = match_result[0]['result']
        running_log.write('找到图片中心坐标: ( ' + str(temp_XY[0]) + ', ' + str(temp_XY[1]) + ')')
        return True
    else:
       running_log.write('未找到。。。')
       return False

if __name__ == '__main__':
    #print(isOnScreen('img/押镖确定.png'))
    #print(IsEscortOK())
    #imgobj = '押镖点传送.png'
    #print(matchImg(imgobj,0.9))
    
    print(matchImg('img/hangzhou.png'))
    print(temp_XY)
    moveMouseTo(int(temp_XY[0]), int(temp_XY[1]))
    